package com.example.yrdev.ordering_system.Report;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.example.yrdev.ordering_system.Activity.ShoppingCartActivity;
import com.example.yrdev.ordering_system.Adapter.GoodAdapter;
import com.example.yrdev.ordering_system.Adapter.GoodTypeAdapter;
import com.example.yrdev.ordering_system.Common.CommonActivity;
import com.example.yrdev.ordering_system.CustomView.GoodsTableView;
import com.example.yrdev.ordering_system.Database.MenuItemTB;
import com.example.yrdev.ordering_system.Dialog.ShoppingCartDialog;
import com.example.yrdev.ordering_system.Model.Good;
import com.example.yrdev.ordering_system.Model.GoodType;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;
import com.example.yrdev.ordering_system.homeOptions.CommodityActivity;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by yrdev on 8/19/16.
 */

public class CommodityFileActivity extends CommonActivity {
    @InjectView(R.id.typeList)ListView typeList;
    @InjectView(R.id.item_detailList)ListView itemDetailList;//商品列表
    @InjectView(R.id.search_txt)
    EditText search_txt;
    @InjectView(R.id.contentContainer)LinearLayout contentContainer;
    @InjectView(R.id.noItem) LinearLayout noItem;
    @InjectView(R.id.shopCartBut)ImageButton shopCartBut;

    GoodTypeAdapter typeAdapter;
    GoodAdapter goodAdapter;
    private  int currentPos=0;

    private ArrayList<Good> arrayList;//商品数组
    private String currentKind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.commodity_purchase_layout);
        ButterKnife.inject(this);
        setBarTitle("商品档案");
        showBackButton();


        shopCartBut.setVisibility(View.GONE);
        initView();




    }
    Good correspondGood;//想买的商品
    public void initView(){


        arrayList=Good.getItemDetailList(this);

        typeAdapter=new GoodTypeAdapter(this);

        typeAdapter.setGoodTypeArrayList(GoodType.getTypeList(this));
        typeList.setAdapter(typeAdapter);

        goodAdapter=new GoodAdapter(this);
        goodAdapter.setGoodArrayList(arrayList);
        itemDetailList.setAdapter(goodAdapter);
        currentKind=typeAdapter.getGoodTypeArrayList().get(0).getId();
        filterGoodBaseOnType(currentKind);

        search_txt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                filterGood(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        typeList.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                try {
                    if (typeList.getChildCount()>0){

                        typeList.getChildAt(currentPos).setSelected(true);

                    }


                }catch (Exception e){

                    GlobalFunction.printTextByTag("error");

                }


            }
        });
        typeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentPos=position;

                selectView(position);
            }
        });
        search_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_DONE){

                    hideKeyboard();





                }
                return false;
            }
        });
        search_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search_txt.setCursorVisible(true);
            }
        });


        itemDetailList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {//商品列表
            correspondGood=goodAdapter.getItem(position);

             showSelection();


            }
        });
    }

    private void showSelection(){
        showDialog();
//        String[] array=new String[]{"补货","删除商品"};
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//
//        builder.setTitle(null)
//                .setItems(array, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        switch (which)
//                        {
//                            case 0:
//                            {
//
//                    showDialog();
//                            }
//                            break;
//                            case 1:
//                            {
//
//                            }
//                            break;
//
//                        }
//                        dialog.dismiss();
//
//                    }
//
//                });
//
//
//        builder.show();

    }
    private void removeGood(){
        goodAdapter.removeGood(correspondGood);

    }


    private   void showDialog(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        LayoutInflater inflater=this.getLayoutInflater();
        View respView=inflater.inflate(R.layout.commodity_ask_amount_layout, null);
        TextView tit=(TextView)respView.findViewById(R.id.tit);
        tit.setText("请输入补货数量");
        // EditText editText=respView.findViewById()
        final EditText amount=(EditText)respView.findViewById(R.id.amount);
        //   amount.setOnTouchListener(editTextOnTouchListener());
        builder.setView(respView).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            try {

                Integer storedNumber=Integer.valueOf(correspondGood.getStoredNumber())+Integer.valueOf(amount.getText().toString());
                correspondGood.setStoredNumber(String.valueOf(storedNumber));
                goodAdapter.notifyDataSetChanged();

                GlobalFunction.ToastAlert(CommodityFileActivity.this,"补货成功");

            }catch (Exception e){

                GlobalFunction.ToastAlert(CommodityFileActivity.this,"补货数量不能为空");
            }



            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();

    }



    private void hideKeyboard(){
        search_txt.setCursorVisible(false);
//        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.hideSoftInputFromWindow(search_txt.getWindowToken(),
//                InputMethodManager.RESULT_UNCHANGED_SHOWN);

    }
    public void filterGood(String string){

        try {
            ArrayList<Good> temp=new ArrayList<>();

            ArrayList<Good> filtered=filterGoodBaseOnType(currentKind);
            if (goodAdapter!=null && goodAdapter.getGoodArrayList().size()>0){//当前分类有对应商品
                for (Good good :filtered){


                    if (good.getName().equals(string)||good.getPinyinID().contains(string)||good.getId().contains(string) && ( good.getKind().equals(currentKind)))
                        temp.add(good);

                }
            }

            if (temp.size()>0)
                goodAdapter.setGoodArrayList(temp);
            else {
                GlobalFunction.printTextByTag("cc");
            }
        }catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());

        }


    }

    private void selectView(int pos){
        try {
            for (int i = 0; i <typeList.getChildCount() ; i++) {
                View view=typeList.getChildAt(i);
                view.setSelected(false);
            }

            View view=typeList.getChildAt(pos);
            view.setSelected(true);

            currentKind=typeAdapter.getGoodTypeArrayList().get(pos).getId();
            filterGoodBaseOnType(currentKind);

//            arrayList.clear();
//            goodAdapter.notifyDataSetChanged();

            //getItemDetail( typeAdapter.getGoodTypeArrayList().get(pos).getId());

        }catch (Exception e){

            GlobalFunction.printTextByTag("error");

        }





    }
    private ArrayList<Good> filterGoodBaseOnType(String type){
        ArrayList<Good> temp=new ArrayList<>();
        for (Good good:arrayList){
            if (good.getKind().equals(type)){
                temp.add(good);

            }
        }

        if (temp.size()==0){

            noItem.setVisibility(View.VISIBLE);
            contentContainer.setVisibility(View.GONE);

        }else {

            noItem.setVisibility(View.GONE);
            contentContainer.setVisibility(View.VISIBLE);
            goodAdapter.setGoodArrayList(temp);
        }


        return temp;

    }



}
